package com.heima.search.service.impl;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.search.entity.ApAssociateWords;
import com.heima.search.service.ApAssociateWordsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ApAssociateWordsServiceImpl implements ApAssociateWordsService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public ResponseResult associateWords(String searchWords) {
        if (StringUtils.isEmpty(searchWords)) {
            return ResponseResult.okResult(null);
        }
        Query query = Query.query(Criteria.where("associateWords").regex(".*?" + searchWords + ".*")).limit(10);
        List<ApAssociateWords> wordsList = mongoTemplate.find(query, ApAssociateWords.class);

        return ResponseResult.okResult(wordsList);
    }
}
